//
//  DDSettingsViewController.m
//  ialone
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 JasperSong. All rights reserved.
//

#import "DDSettingsViewController.h"
#import "DDSettingsModel.h"
#import "DDAboutViewController.h"
#import "DDManager.h"
#import "FMDB.h"
@interface DDSettingsViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, strong) FMDatabase *db;
@end

@implementation DDSettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self addTableView];
    //[self setUpData];
    
    self.dataArray = @[@"清除缓存",@"关于",@"退出登录"];
    
    
    
    [self.leftButton setImage:[UIImage imageNamed:@"fanhui"] forState:(UIControlStateNormal)];
    [self.leftButton addTarget:self action:@selector(leftButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.titleLabel.text = @"设置";
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"DDDataBase.db"];
    
    _db = [FMDatabase databaseWithPath:path];
    //调用open方法打开数据库,成功返回YES,失败返回NO
    if ([_db open]) {
        NSLog(@"打开成功");
    }else
    {
        NSLog(@"打开失败");
    }
    
//    // 左边返回按钮
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"fanhui" ] style:(UIBarButtonItemStylePlain) target:self action:@selector(back:)];
    
}
- (void)leftButton:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}
//- (void)back:(UIButton *)button{
//    self.navigationController.navigationBarHidden = YES;
//    [self.navigationController popViewControllerAnimated:YES];
//}

//- (void)setUpData
//{
//    NSString *path = [[NSBundle mainBundle]pathForResource:@"new_personCenter" ofType:@"plist"];
//    
//    NSArray *array = [NSArray arrayWithContentsOfFile:path];
//    self.dataArray = [NSMutableArray array];
//    
//    for (NSArray *oneArray in array) {
//        NSMutableArray *arr = [NSMutableArray array];
//        for (NSDictionary *oneDic in oneArray) {
//            DDSettingsModel *model = [[DDSettingsModel alloc]init];
//            [model setValuesForKeysWithDictionary:oneDic];
//            [arr addObject:model];
//            
//        }
//        
//        [self.dataArray addObject:arr];
//    }
//    
//}

- (void)addTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:(UITableViewStyleGrouped)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"MyCell"];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"MyCell"];

    
    
    cell.textLabel.text = self.dataArray[indexPath.row];
    
    if (indexPath.row == 0) {
        cell.detailTextLabel.text = [self butt];
    };
    
    
    
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
        [self.navigationController popViewControllerAnimated:YES];
        
    } else if (indexPath.row == 1) {
        
        DDAboutViewController *abVC = [[DDAboutViewController alloc]init];
        
        [self.navigationController pushViewController:abVC animated:YES];
        
    }else{
        if ([[DDManager sharedHandle] islogin] == YES) {
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"islogin"];
            [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"username"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"退出登录" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
            [alter show];
            [UIView animateWithDuration:1 animations:^{
                [alter dismissWithClickedButtonIndex:0 animated:YES];
            }];
            [self.navigationController popToRootViewControllerAnimated:YES];
            
        }else
        {
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请先登录" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
            [alter show];
            [UIView animateWithDuration:1 animations:^{
                [alter dismissWithClickedButtonIndex:0 animated:YES];
            }];
        }
    }
}


- (NSString *)butt{
    
    
    NSString *result ;
//    NSString *path = NSHomeDirectory();
     NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"%@",path);
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:path]){
        float folderSize;
        if ([fileManager fileExistsAtPath:path]) {
            NSArray *childerFiles=[fileManager subpathsAtPath:path];
            for (NSString *fileName in childerFiles) {
                NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
                long long size=[fileManager attributesOfItemAtPath:absolutePath error:nil].fileSize;
                folderSize += size;
            }
    
            
            CGFloat num = 0.0;
            if (folderSize/1024.0/1024> 1){
                num = folderSize/1024.0/1024;
                NSString *str = [NSString stringWithFormat:@"%.1fM",num];
                result = str;
                
            }else{
                num = folderSize /1024;
                 NSString *str = [NSString stringWithFormat:@"%.1fKB",num];
                result = str;
               
            }
        }
        
    }
    return  result;
    
}


//- (void)calculate


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
